package com.wuyoucar.service.impl;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TUser;
import com.wuyoucar.entity.TUserinfo;
import com.wuyoucar.mapper.TUserMapper;
import com.wuyoucar.mapper.TUserinfoMapper;
import com.wuyoucar.service.ITUserService;
import com.wuyoucar.util.MD5;
import com.wuyoucar.util.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

@Service
public class TUserServiceImpl implements ITUserService {

    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TUserinfoMapper tUserinfoMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 使用用户名和密码进行登录
     * @param tUser
     * @return
     */
    @Override
    public ResultBean getUser(TUser tUser) {
        TUser user = tUserMapper.getUser(tUser);
        if(tUser != null && user != null){
            if(MD5.MD5(tUser.getPassword()).equalsIgnoreCase(user.getPassword())){
                return new ResultBean(200,"登录成功",user);
            }
        }
        return new ResultBean(404,"用户名或密码错误",user);
    }

    //使用手机验证码进行登录
    @Override
    public ResultBean getUserByTel(String usertel, String code) {
        if(code.equals("")){
            return new ResultBean(404,"请输入验证码");
        }
        TUser user = tUserMapper.getUserByTel(usertel);
        String lcode = stringRedisTemplate.opsForValue().get("lcode"+usertel);
        if(lcode.equals(code)){
            if(user == null){
                tUserMapper.addUserByTel(usertel);
                //将用户详情写入数据库
                tUserinfoMapper.insertTUserinfo(  new TUserinfo(tUserMapper.getUserByTel(usertel).getUserid()));

            }
            return new ResultBean(200,"登录成功",user);
        }
        return new ResultBean(404,"验证码错误");
    }



    /**
     * 用户使用手机号进行注册
     * @param usertel 手机号
     * @param code 输入的验证码
     * @return
     */
    @Override
    public ResultBean addUserByTel(String usertel, String code) {
        //查询该手机号是否已经被注册过
       TUser user = tUserMapper.getUserByTel(usertel);
       //如果被注册过，返回手机号已经被注册，请使用该手机号注册
       if(user != null){
           return new ResultBean(404,"手机号已经被注册，请使用该手机号注册",user);
       }else {
           //从redis中获取验证码
           String vcode = stringRedisTemplate.opsForValue().get("vcode" + usertel);
           //判断输入的验证码和生成的验证码是否一致
           if(vcode.equals(code)){
               //将用户信息写入数据库
               tUserMapper.addUserByTel(usertel);
               //将用户详情写入数据库
               tUserinfoMapper.insertTUserinfo(  new TUserinfo(tUserMapper.getUserByTel(usertel).getUserid()));
                return new ResultBean(200,"注册成功");
           }
           return new ResultBean(404,"验证码错误，请重新输入");
       }

    }

    //获取手机注册验证码
    @Override
    public ResultBean getCode(String usertel) {
        if(usertel == null || usertel.equals("")){
            return new ResultBean(404,"手机号不能为空");
        }else{
            //设置位数随机验证码并写到redis中，设置过期时间为5分钟
            String vcode = "123456";
            //String lcode = PhoneCode.vcode(); //6位数随机验证码
            //发送验证码
            //PhoneCode.getPhonemsg(usertel,lcode);
            stringRedisTemplate.opsForValue().set("vcode"+usertel,vcode,300,TimeUnit.SECONDS);
            return new ResultBean(0,"验证码获取成功",vcode);
        }
    }

    //获取手机登录的验证码
    @Override
    public ResultBean loginCode(String usertel) {
        if(usertel == null || usertel.equals("")){
            return new ResultBean(404,"手机号不能为空");
        }else{
            String lcode = "123456";
            //String lcode = PhoneCode.vcode(); //6位数随机验证码
            //发送验证码
            //PhoneCode.getPhonemsg(usertel,lcode);
            stringRedisTemplate.opsForValue().set("lcode"+usertel,lcode,300,TimeUnit.SECONDS);
            return new ResultBean(200,"验证码获取成功",lcode);
        }
    }

    @Override
    public ResultBean addUserByName(String username,String password1, String password2) {
            if (password1.equals(password2)){
               if(tUserMapper.selectByName(username) == null) {
                   //将用户查询数据库
                   if (tUserMapper.addUserByName(username,password2) >0){
                       //将用户详情查询数据库
                       tUserinfoMapper.insertTUserinfo(new TUserinfo(tUserMapper.selectByName(username).getUserid()));
                       return new ResultBean(200,"注册成功");
                   }else{
                       return new ResultBean(404,"注册失败");
                   }
               }else{
                   return new ResultBean(300,"该用户名已被注册,操作失败");
               }
            }else{
                return new ResultBean(400,"两次密码不一致,请重新输入");
            }
    }
}
